package com.ruoyi.web.controller.business.sundry;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.sundry.domain.JbRawMaterialPriceHistory;
import com.ruoyi.business.sundry.domain.vo.JbRawMaterialPriceHistoryVo;
import com.ruoyi.business.sundry.service.JbRawMaterialPriceHistoryService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 原材料价格历史 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("原材料价格历史")
@RestController
@RequestMapping("/rawmaterial_price_history")
public class JbRawMaterialPriceHistoryController extends BaseController
{
    @Autowired
    private JbRawMaterialPriceHistoryService service;

    /**
     * 查询原材料价格历史列表
     */
    @ApiOperation("查询原材料价格历史列表")
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbRawMaterialPriceHistory jbRawMaterialPriceHistory)
    {
        LambdaQueryWrapper<JbRawMaterialPriceHistory> select = new LambdaQueryWrapper<>();
        IPage<JbRawMaterialPriceHistory> iPage = service.page(page.b(), select);
        return JoR.rows(iPage);
    }

    /**
     * 导出原材料价格历史列表
     */
    @ApiOperation("导出原材料价格历史列表")
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:export')")
    @Log(title = "原材料价格历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page,
                       JbRawMaterialPriceHistory jbRawMaterialPriceHistory)
    {
        LambdaQueryWrapper<JbRawMaterialPriceHistory> select = new LambdaQueryWrapper<>();
        IPage<JbRawMaterialPriceHistory> iPage = service.page(page.b(), select);

        List<JbRawMaterialPriceHistory> list = iPage.getRecords();
        ExcelUtil<JbRawMaterialPriceHistory> util =
                new ExcelUtil<JbRawMaterialPriceHistory>(JbRawMaterialPriceHistory.class);
        util.exportExcel(response, list, "原材料价格历史数据");
    }

    /**
     * 获取原材料价格历史详细信息
     */
    @ApiOperation("获取原材料价格历史详细信息")
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增原材料价格历史
     */
    @ApiOperation("新增原材料价格历史")
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:add')")
    @Log(title = "原材料价格历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbRawMaterialPriceHistory jbRawMaterialPriceHistory)
    {
        return toAjax(service.save(jbRawMaterialPriceHistory));
    }

    /**
     * 修改原材料价格历史
     */
    @ApiOperation("修改原材料价格历史")
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:edit')")
    @Log(title = "原材料价格历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbRawMaterialPriceHistory jbRawMaterialPriceHistory)
    {
        return toAjax(service.updateById(jbRawMaterialPriceHistory));
    }

    /**
     * 删除原材料价格历史
     */
    @ApiOperation("删除原材料价格历史")
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:remove')")
    @Log(title = "原材料价格历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("查询原材料价格历史列表")
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:list')")
    @GetMapping("/listData")
    public AjaxResult listData(PageParam page, JbRawMaterialPriceHistory jbRawMaterialPriceHistory)
    {
        List<JbRawMaterialPriceHistoryVo> list = service.listData(page, jbRawMaterialPriceHistory);
        return success(list);
    }

    @ApiOperation("导出查询原材料价格历史列表")
    @Log(title = "导出查询原材料价格历史列表", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('rawmaterial_price_history:export')")
    @PostMapping("/importListData")
    public void importListData(HttpServletResponse response, PageParam page,
                               JbRawMaterialPriceHistory jbRawMaterialPriceHistory)
    {
        List<JbRawMaterialPriceHistoryVo> list = service.listData(page, jbRawMaterialPriceHistory);
        ExcelUtil<JbRawMaterialPriceHistoryVo> util =
                new ExcelUtil<JbRawMaterialPriceHistoryVo>(JbRawMaterialPriceHistoryVo.class);
        util.exportExcel(response, list, "原材料价格历史数据导出");
    }
}
